package template;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.util.*;


/**
 * Created by WangZiHe on 2017/11/8
 * QQ/WeChat:648830605
 * QQ-Group:368512253
 * Blog:www.520code.net
 * Github:https://github.com/yancheng199287
 */

public class AppConf {
    //D:\workspace\AiShangCinema
    //项目路径
    private String PROJECT_PATH = System.getProperty("user.dir");
    //源代码路径
    private String SOURCE_PATH = PROJECT_PATH + "/src/main/java";
    //包名 如果包名发生变化 需要修改
    private String PACKAGE = "com.oneinlet";
    private String PACKAGE_PATH = SOURCE_PATH + "/com/oneinlet";

    private String BEAN_PACKAGE = PACKAGE + ".entity";
    private String CONTROLLER_PATH = PACKAGE_PATH + "/controller";
    private String SERVICE_PATH = PACKAGE_PATH + "/service";
    private String DAO_PATH = PACKAGE_PATH + "/dao";


    private String DAO_PACKAGE_NAME = PACKAGE + ".dao";

    private String SERVICE_PACKAGE_NAME = PACKAGE + ".service";

    private String CONTROLLER_PACKAGE_NAME = PACKAGE + ".controller";

    private String beanName = null;
    private String beanPackage = null;

    private Map<String, Object> rootData = new HashMap<>();  //注意必须有一个根结点 data-model


    /**
     * @param beanName          实体类名称
     * @param isAddChildPackage 是否建立子包名，默认 规则是实体类名（小写）
     */
    public void letgo(String beanName, Boolean isAddChildPackage) {
        rootData.put("beanName", beanName);
        this.beanName = beanName;
        this.beanPackage = BEAN_PACKAGE + "."+beanName;
        if (isAddChildPackage) {
            String beanNameLower = beanName.toLowerCase();
            CONTROLLER_PATH += "/" + beanNameLower;
            SERVICE_PATH += "/" + beanNameLower;
            DAO_PATH += "/" + beanNameLower;
        }
        makeDaoFile();
        makeServiceFile();
        makeControllerFile();
    }

    private void makeDaoFile() {
        Template template = getTemplate("DAO.ft");
        String outPath = DAO_PATH + "/" + beanName + "DAO.java";
        rootData.put("packageName", DAO_PACKAGE_NAME);
        generateFileByTemplate(template, outPath, rootData);
    }

    private void makeServiceFile() {
        Template template = getTemplate("Service.ft");
        String outPath = SERVICE_PATH + "/" + beanName+"Service.java";
        rootData.put("packageName", SERVICE_PACKAGE_NAME);
        rootData.put("daoPackage", DAO_PACKAGE_NAME);
        rootData.put("servicePackage", SERVICE_PACKAGE_NAME);
        rootData.put("package", PACKAGE);
        generateFileByTemplate(template, outPath, rootData);
    }


    private void makeControllerFile() {
        Template template = getTemplate("Controller.ft");
        String outPath = CONTROLLER_PATH + "/" + beanName + "Controller.java";
        rootData.put("packageName", CONTROLLER_PACKAGE_NAME);
        rootData.put("daoPackage", DAO_PACKAGE_NAME);
        rootData.put("servicePackage", SERVICE_PACKAGE_NAME);
        rootData.put("package", PACKAGE);
        generateFileByTemplate(template, outPath, rootData);
    }

    private void generateFileByTemplate(Template template, String outPath, Map<String, Object> dataMap) {
        try (FileOutputStream fos = new FileOutputStream(outPath)) {
            dataMap.put("beanPackage", beanPackage);
            dataMap.put("beanName", beanName);
            dataMap.put("createTime", new Date());
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
            template.process(dataMap, out);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private Template getTemplate(String name) {
        try {
            //通过Freemarker的Configuration读取相应的ftl
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);//这里是对应的你使用jar包的版本号：<version>2.3.23</version>
            //configuration.setDirectoryForTemplateLoading(new File("/ftl")); //如果是maven项目可以使用这种方式
            //第二个参数 为你对应存放.ftl文件的包名
            String templatePath = this.getClass().getResource("").getPath();
            configuration.setDirectoryForTemplateLoading((new File(templatePath)));
            return configuration.getTemplate(name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}